<?php

namespace app\backend\model;

use think\Model;
use think\model\concern\SoftDelete;

class User extends Model
{

    protected $table = 'blog_users';

    //软删除
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    //自动生成属性salt
    protected $auto = ['salt', 'last_login_time', 'last_login_ip'];

    //自动更新最后一次登录时间时间, 登录的IP地址
    protected $update = ['last_login_time', 'last_login_ip'];

    //加密盐
    public function setSaltAttr()
    {
        $str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890';
        $salt = substr(str_shuffle($str),10,6);
        return $salt;
    }

    //最新登录时间
    public function setLastLoginTime()
    {
        return time();
    }

    //登录的IP地址
    public function setLastLoginIp()
    {
        return request()->ip();
    }

    //密码加密MD5
    public function setPasswordAttr($value)
    {
        $salt = self::setSaltAttr();
        $value = $value . $salt;
        $value = md5($value);

        return $value;
    }


    //一个用户对应一个用户权限组
    public function role()
    {
        return $this->hasOne('Role', 'id', 'role_id');
    }

}
